/**
* dataInputChart functions
* copyright@Catosoft.com reserved, 2016
* 2016.06.20, created by Eric.Wang
*/



/*
* get data
*/
var getDataInputColumnAndPieData = function(){
	var data = {

	};
	var option = {
		url: '/draft/data/input/v1',
		type: 'GET',
		data: data,
	};
	createAjax(option, function(err, res){
		if(!err){
			dataInputColumnChart('dataInputColumnChart', res);
			dataInputPieChart('dataInputPieChart', res);
			dataInputCountTable('dataInputCountTable', res);
			dataInputTimeTable('dataInputTimeTable', res);
		}
	});
}

/*
* data filter
*/
var dataInputColumnFilter = function(data){
	var series = [{
		name: '应录入表单数',
		identifier: 'should',
		data: [],
	}, {
		name: '已录入表单数',
		identifier: 'already',
		data: [],
	}];
	var categories = [];
	columnFilter(data, series, categories);
	return {
		series: series,
		categories: categories,
	};
}

/*
* draw charts
*/
var dataInputColumnChart = function(element, data){
	var filter = dataInputColumnFilter(data.list);
	var dataInputColumnChartOption = {
		chart: {
	        type: 'column',
	        renderTo: element,
	        zoomType: 'x'
	    },
	    colors: colors[0],
		title: {
	        text: chartTitles.dataInputColumn,
	    },
	    xAxis: {
	    	title: {
	    		text: '中心',
	    	},
	    	type: 'category',
	    	categories: filter.categories,
	    },
	    yAxis: {
	        title: {
	            text: '数量',
	        },
	        plotLines: [{
	            value: 0,
	            width: 1,
	            color: '#808080'
	        }]
	    },
	    legend: {
	        layout: 'vertical',
	        align: 'right',
	        verticalAlign: 'middle',
	        borderWidth: 0
	    },
	    series: filter.series,
	    credits: credits,
	    exporting: {
	    	buttons: {
	    		contextButton: {
	    			menuItems: getMenuItems(),
	    		}
	    	}
	    },
	};
	var chart = new Highcharts.Chart(dataInputColumnChartOption);
	bindSelect(element, chart, filter);
}

var dataInputPieChart = function(element, data){
	var dataInputPieChartOption = {
		chart: {
	        renderTo: element,
	        plotShadow: true
	    },
	    colors: colors[0],
		title: {
	        text: chartTitles.dataInputPie,
	    },
	    plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    color: '#000000',
                    connectorColor: '#000000',
                    format: '<b>{point.name}</b>: {point.percentage:.1f} %'
                }
            }
        },
	    legend: {
	        layout: 'vertical',
	        align: 'right',
	        verticalAlign: 'middle',
	        borderWidth: 0
	    },
	    series: [{
	    	type: 'pie',
	    	name: '人数',
	    	data: [
	    		['应录入表单数', data.list[0].should],
	    		['已录入表单数', data.list[0].already],
	    	]
	    }],
	    credits: credits,
	    exporting: {
	    	buttons: {
	    		contextButton: {
	    			menuItems: getMenuItems(),
	    		}
	    	}
	    }
	};
	var chart = new Highcharts.Chart(dataInputPieChartOption);
}

var dataInputCountTable = function(element, data){
	$('#' + element).bootstrapTable({
		data: data.list,
		classes: 'table table-hover',
		pagination: false,
		pageSize: 20,
		pageNumber: 1,
		pageList: [20, 50],
		sidePagination: 'client',
		showRefresh: true,
		showColumns: true,
		buttonsAlign: 'right',
		showExport: true,
		exportDataType: 'all',
		exportTypes: ['csv', 'txt', 'excel'],
		locale: 'zh-CN',
		columns: [
		{
			field: 'Number',  
	    title: '序号',  
	    formatter: function (value, row, index) {  
	    	return index + 1;  
	    }
		},
		{
			title: '中心',
			field: 'name'
		}, {
			title: '应录入表单数',
			field: 'should'
		}, {
			title: '已录入表单数',
			field: 'already'
		},  {
			title: '录入率',
			field: 'rate'
		}],
	});
}

var dataInputTimeTable = function(element, data){
	$('#' + element).bootstrapTable({
		data: data.list,
		classes: 'table table-hover',
		pagination: false,
		pageSize: 20,
		pageNumber: 1,
		pageList: [20, 50],
		sidePagination: 'client',
		showRefresh: true,
		showColumns: true,
		buttonsAlign: 'right',
		showExport: true,
		exportDataType: 'all',
		exportTypes: ['csv', 'txt', 'excel'],
		locale: 'zh-CN',
		columns: [
			{
				field: 'Number',  
		    title: '序号',  
		    formatter: function (value, row, index) {  
		    	return index + 1;  
		    }
			},
			{
				title: '中心',
				field: 'name'
			},
			{
				title: '首次录入时间',
				field: 'start_time'
			},
			{
				title: '末次录入时间',
				field: 'end_time'
			}
		]
	});
}

$(function(){
	getDataInputColumnAndPieData();
});